﻿using System;
using System.Linq;
using System.Data;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;

namespace SedionFormDesigner.CusControls.TextBox
{
    public partial class FrmFormular : Form
    {
        private String formular;
        private List<Control> forms;
        private BD_TextBox txtbox;

        private Button[] btns;
        private Control dropElement = null;

        public FrmFormular(BD_TextBox txtbox, String formular)
        {
            InitializeComponent();
            this.forms = SedionDesignerManager.Forms;
            this.txtbox = txtbox;
            this.formular = formular;
            this.txtformular.Text = formular;
            this.bindButton();
            this.lblcellformular.Text = txtbox.Name + "公式";
            this.addTextbox();
        }

        public String getFormular()
        {
            this.DialogResult = DialogResult.OK;
            this.formular = txtformular.Text;
            this.formular = formular.Replace("  ", " ");
            if (formular.StartsWith(" "))
                this.formular = formular.Substring(1);
            if (formular.EndsWith(" "))
                this.formular = formular.Substring(0, this.formular.Length - 1);
            return formular;
        }

        private void addTextbox()
        {
            List<Control> ctls = new List<Control>();
            foreach (Control form in forms)
            {
                var ctrls = form.Controls;
                foreach (Control ctrl in ctrls)
                {
                    if (ctrl.GetType() == typeof(BD_TextBox))
                    {
                        if (!ctrl.Name.Contains(" number"))
                            ctrl.Name = String.Concat(ctrl.Name, " number");
                        ctls.Add(ctrl);
                    }
                }

                foreach (Control ctrl in ctrls)
                {
                    if (ctrl.GetType() == typeof(BD_DateTimePicker))
                    {
                        if (!ctrl.Name.Contains(" date"))
                            ctrl.Name = String.Concat(ctrl.Name, " date");
                        ctls.Add(ctrl);
                    }
                }
            }

            setTextbox(ctls);

        }

        private void setTextbox(List<Control> txts)
        {
            const Int32 lblwidth = 123;
            const Int32 lblheight = 20;
            const Int32 rowcount = 6;

            Int32 x = 10;
            Int32 y = 10;
            for (int index = 0; index < txts.Count; index++)
            {
                var lable = new Label();
                lable.MouseDown += new MouseEventHandler(lable_MouseDown);
                lable.Size = new Size(lblwidth, lblheight);
                lable.BorderStyle = BorderStyle.FixedSingle;
                this.panel1.Controls.Add(lable);
                lable.Location = new Point(x, y);
                lable.TextAlign = ContentAlignment.MiddleCenter;
                lable.Text = txts[index].Name;
                if (lable.Text.Contains(" date"))
                    lable.BackColor = Color.Gray;

                x += (lblwidth + 10);
                if ((index + 1) % rowcount == 0)
                {
                    y += (lblheight + 10);
                    x = 10;
                }
            }
        }

        void lable_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var lbl = (Label)sender;
                dropElement = lbl;

                lbl.DoDragDrop(lbl, DragDropEffects.Copy | DragDropEffects.Move);
            }
        }

        private void bindButton()
        {
            btns = new Button[15];
            btns[0] = this.button1;
            btns[1] = this.button2;
            btns[2] = this.button3;
            btns[3] = this.button4;
            btns[4] = this.button5;
            btns[5] = this.button6;
            btns[6] = this.button7;
            btns[7] = this.button8;
            btns[8] = this.button9;
            btns[9] = this.button10;
            btns[10] = this.button11;
            btns[11] = this.button12;
            btns[12] = this.button13;
            btns[13] = this.button14;
            btns[14] = this.button15;

            setButtonEvent();
        }

        private void setButtonEvent()
        {
            for (int index = 0; index < btns.Length; index++)
            {
                btns[index].MouseDown += new MouseEventHandler(Btn_MouseDown);
            }
        }

        private void Btn_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                var btn = (Button)sender;
                dropElement = btn;

                btn.DoDragDrop(btn, DragDropEffects.Copy | DragDropEffects.Move);
            }

        }

        private void FrmFormular_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(typeof(Button)))
                e.Effect = DragDropEffects.Copy;
            if (e.Data.GetDataPresent(typeof(Label)))
                e.Effect = DragDropEffects.Copy;
        }

        private void FrmFormular_DragDrop(object sender, DragEventArgs e)
        {
            if (dropElement.GetType() == typeof(Button))
            {
                var oper = "/*-+";
                var text = dropElement.Text;
                if (oper.Contains(text))
                    text = " " + text + " ";

                this.txtformular.Text += text;
            }
            else if (dropElement.GetType() == typeof(Label))
            {
                var text = dropElement.Text;
                text = " [" + text + "] ";
                this.txtformular.Text += text;
            }
            dropElement = null;
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btndone_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
